package ZhaoShang;


import java.util.Arrays;
import java.util.Random;

/**
 * Description：博弈论 从一个方向 取数，一次可以取一个或两个，求最终谁获胜
 * Author: zhangc
 * Date：2017/9/13 19:32
 */
public class Boyi {

    public static void main(String[] args) {
        int[] arr = {2, 1, 4, 5, 7, 9, 6};
        Random random = new Random();
        for (int i = 0; i < 30; i++) {
            int num = random.nextInt(20);
            int[] array = new int[ num ];
            for (int j = 0; j < num; j++) {
                array[ j ] = random.nextInt(30);
            }
            System.out.println(Arrays.toString(array));
            win(array);
        }
    }


    private static void win(int[] arr) {
        int sum = Arrays.stream(arr).sum();
        int process = process(arr, 0, arr.length - 1);
        if (2 * process == sum) {
            System.out.println("-----平了-----");
            return;
        }
        System.out.println(2 * process > sum);
        System.out.println("----------------------------");
    }


    public static int process(int[] arr, int i, int j) {
        if (i > j)
            return 0;
        if (i == j)
            return arr[ i ];
        if (i == j - 1)
            return arr[ i ] + arr[ j ];
        if (i + 3 == j)
            return Math.max(arr[ i ] + arr[ i + 3 ], arr[ i ] + arr[ i + 1 ]);
        return Math.max(arr[ i ] + Math.min(process(arr, i + 2, j), process(arr, i + 3, j)),
                arr[ i ] + arr[ i + 1 ] + Math.min(process(arr, i + 3, j), process(arr, i + 4, j))
        );
    }

}
